package android.slc.fast.vd.list

import android.slc.adapter.PageModel
import javafx.event.EventHandler
import javafx.scene.Node
import javafx.scene.control.ScrollPane
import javafx.scene.input.MouseEvent
import javafx.scene.layout.GridPane
import org.apache.commons.lang3.StringUtils
import java.io.Serializable

abstract class BaseGridPageViewVdByFx<T : Serializable, VB : BaseListPageVmSubByFx<T>> : BaseListPageVdByFx<T, VB>(),
    BaseGridViewVdIface<T> {

    protected lateinit var scrollPane: ScrollPane
        private set

    private lateinit var gridPane: GridPane

    protected val paginationViewHelper = PaginationViewHelper()

    override fun initView() {
        super.initView()
        paginationViewHelper.initPagination(contentView,vmSub)
    }

    override fun initSwipeRecycler() {
        super.initSwipeRecycler()
        if (StringUtils.isEmpty(getScrollPaneId())) {
            this.gridPane = contentView.lookup(getGridPaneId()) as GridPane
        } else {
            this.scrollPane = contentView.lookup(getScrollPaneId()) as ScrollPane
            this.gridPane = scrollPane.content.lookup(getGridPaneId()) as GridPane
        }
    }

    protected open fun onItemClick(item: T?) {
        item?.let {
            this.onAdapterItemClickListener?.onItemClick(item)
        }
    }

    override fun loadMoreComplete(pageModel: PageModel<T>) {
        super.loadMoreComplete(pageModel)
        paginationViewHelper.loadMoreComplete(pageModel)
        loadItemBefore(pageModel.listNoNull)
        loadItem(pageModel.listNoNull)
    }

    protected val itemViewClick: EventHandler<MouseEvent> = EventHandler {
        onItemClick((it.source as Node).userData as T?)
    }

}
